package com.ruoyi.framework.interceptor;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.RedisService;
import com.ruoyi.common.utils.StrKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Writer;

/**
 * 供应商拦截器
 */
@Component
public class SupplyInterceptoer implements HandlerInterceptor {

    @Autowired
    RedisService redis;

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean ifPass = false;

        String url=request.getRequestURI();
        String token=request.getHeader("token");

        System.out.println("url : "+url);
        if(url.endsWith("/api/supply/userCenter/register")//注册
                ||url.endsWith("/api/supply/userCenter/login")//登录
                ||url.endsWith("/api/supply/userCenter/getProtocal")//获取协议
                ||url.endsWith("/api/supply/userCenter/sendMsg")//发送验证码
                ||url.contains("/api/common/download")//通用下载
                ||url.contains("/api/common/upload")//通用上传
                ||url.contains("/api/common/uploads")//通用上传
                ||url.contains("/api/common/resource")//通用资源下载
        )//不经过过滤器的路径
        {
            System.out.println("通过过滤器");
            ifPass=true;
        }else{
//			System.out.println("chekcKey:"+chekcKey);
            if(StrKit.notBlank(token))
            {
                //测试token
                if(token.equals(Constants.testToken))return true;

                if(redis.getVal(Constants.SUPPLY_TOKEN_KEY+token)!=null)return true;
                if(redis.getVal(Constants.SHOP_TOKEN_KEY+token)!=null)return true;
            }else{
                Writer writer=response.getWriter();

                AjaxResult ret=new AjaxResult();
                ret.put("code",-1);
                ret.put("msg","请先登录");
                writer.write(ret.toString());
                writer.close();
            }
        }

        //处理注解
        if(handler instanceof HandlerMethod){


        }

        return ifPass;
    }

}
